package com.enzoism.springboot.fastjson.service;

import com.enzoism.springboot.fastjson.model.Scores;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业务操作Service
 */
@Service
public class ScoresService {
    @Resource
    public ScoresDBService scoresDBService;

    /**
     * 列表
     */
    public List<Scores> selectDataList() {
        return scoresDBService.selectDataList();
    }

    /**
     * 详情
     */
    public Scores selectByPrimaryKey(Integer id) {
        return scoresDBService.selectByPrimaryKey(id);
    }

    /**
     * 更新
     */
    @Transactional
    public Scores updateByPrimaryKey(Scores scores) {
        // 1-先进行对象更新
        scoresDBService.updateByPrimaryKey(scores);
        // 2-进行数据查询返回
        return selectByPrimaryKey(scores.getId());
    }


    /**
     * 删除重插
     */
    @Transactional
    public Scores reinsertByPrimaryKey(Scores scores) {
        // 1-先进行对象更新
        scoresDBService.deleteByPrimaryKey(scores);
        // 2-重新插入对象-模拟业务逻辑失败
        Integer scoreId = scoresDBService.insertAndReturnAutoId(scores);
        // 3-进行数据查询返回
        return selectByPrimaryKey(scoreId);
    }

    /**
     * 删除重插-模拟业务逻辑失败
     */
    @Transactional
    public Scores reinsertByPrimaryKeyRuntimeException(Scores scores) {
        // 1-先进行对象更新
        scoresDBService.deleteByPrimaryKey(scores);
        // 2-重新插入对象-模拟业务逻辑失败
        Integer scoreId = scoresDBService.insertAndReturnAutoIdRuntimeException(scores);
        // 3-进行数据查询返回
        return selectByPrimaryKey(scoreId);
    }

    /**
     * 删除重插-真实数据库插入失败
     */
    @Transactional
    public Scores reinsertByPrimaryKeySQLException(Scores scores) {
        // 1-先进行对象更新
        scoresDBService.deleteByPrimaryKey(scores);
        // 2-重新插入对象-真实数据库插入失败
        Integer scoreId = scoresDBService.insertAndReturnAutoIdSQlException(scores);
        // 3-进行数据查询返回
        return selectByPrimaryKey(scoreId);
    }
}
